package com.thon.controller.rest;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.common.collect.Maps;
import com.thon.entity.project.Project;
import com.thon.entity.project.ProjectRecommend;
import com.thon.entity.user.User;
import com.thon.service.project.ProjectRecommendService;
import com.thon.service.project.ProjectService;

/**
 * @file ProjectRecommendRestController.java
 * @author Zemo
 * @mail zemochen@gmail.com
 * @data 2013年11月18日 下午2:35:26
 * @description: TODO
 */
@Controller
@RequestMapping(value = "/api/project")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class ProjectRecommendRestController {

	@Autowired
	private ProjectService projectService;
	@Autowired
	private ProjectRecommendService projectRecommendService;

	// 根据项目id添加推荐项目并更新project表中的推荐数
	@RequestMapping(value = "/recommend", method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> recommendProject(
			@Valid @RequestBody ProjectRecommend projectRecommend) {

		int pid = projectRecommend.getProject().getId();
		int uid = projectRecommend.getUser().getId();
		ProjectRecommend oldProjectRecommend = projectRecommendService
				.findProjectRecommend(pid, uid);
		Map<String, Object> map = Maps.newHashMap();
		String msg="";
		if (oldProjectRecommend == null) {
			projectRecommendService.add(projectRecommend);

			Project project = new Project();
			project = projectService.getProject(-1, pid);
			Integer recommendCount = projectRecommendService
					.countProjectRecommend(pid).intValue();
			project.setRecommendCount(recommendCount);
			projectService.updateProject(project);
			msg="Recommend Success";
		} else {
			msg="Recommend Failure";
		}
		map.put("msg", msg);
		return new ResponseEntity(map,HttpStatus.OK);

	}

	// 通过project id 获取推荐该项目的社会责任家
	@RequestMapping(value = "/recommender")
	public ResponseEntity<HttpEntity> listRecommender(
			@RequestParam(value = "pid") int projectId, Model model) {

		List<ProjectRecommend> projectRecommends = projectRecommendService
				.listRecommenders(projectId);
		List<User> recommenders = new ArrayList<User>();
		String name = null;
		for (int i = 0; i < projectRecommends.size(); i++) {
			name = projectRecommends.get(i).getUser().getName();
			User user = new User();
			user.setName(name);

			recommenders.add(user);
		}
		model.addAttribute("recommenders", recommenders);
		return new ResponseEntity(model, HttpStatus.OK);
	}

	// 通过用户id、项目id查询该项目是否已经被该用户推荐
	@RequestMapping(value = "/recommend/check", method = RequestMethod.GET)
	public ResponseEntity<HttpEntity> checkProjectIsRecommended(
			@RequestParam(value = "pid", required = true) Integer pid,
			@RequestParam(value = "uid", required = true) Integer uid) {

		if (uid != null
				&& projectRecommendService.findProjectRecommend(pid, uid) != null) {
			return new ResponseEntity(1, HttpStatus.OK);
		} else {
			return new ResponseEntity(0, HttpStatus.OK);
		}
	}

}
